// an order 1 decoder
// For a 32 speaker array

desc: Wig_1o_MS216 WigWare Ambi Order 1, 1H1V 32 Speaker Decoder

//include Ambisonic Filter Functions
import WigAmbFilter.jsfx-inc

//include decoder filenames
filename:0,Wig_1o_MS216_pic_1.png
filename:1,Wig_1o_MS216_pic_2.png
filename:2,Wig_1o_MS216_pic_3.png
filename:3,UoD_Logo_Small.png

slider1:0<0,2,1{Strict,Cardioid,Energy}>LF Decode Type
slider2:2<0,2,1{Strict,Cardioid,Energy}>HF Decode Type
slider3:400<100,1000,20>Transistion Frequency (Hz)
slider4:LFGain=0.0<-6.0,6.0,0.05>LF Decoder Gain (dB)
slider5:HFGain=1.8799<-6.0,6.0,0.05>HF Decoder Gain (dB)
slider6:DistFiltEnable=0<1,2,1{Off,Speaker Distance Comp (SDC),SDC & NFC}>Distance Filtering
slider7:SpDist=4<0.5,4,0.05>Speaker Distance
slider8:NFCDist=4<0.5,4,0.05>Near-Field Comp Distance

in_pin:0_AmbiX
in_pin:1_AmbiX
in_pin:2_AmbiX
in_pin:3_AmbiX
out_pin:0 az 0 el
out_pin:22.5 az 0 el
out_pin:45 az 0 el
out_pin:67.5 az 0 el
out_pin:90 az 0 el
out_pin:112.5 az 0 el
out_pin:135 az 0 el
out_pin:157.5 az 0 el
out_pin:180 az 0 el
out_pin:202.5 az 0 el
out_pin:225 az 0 el
out_pin:247.5 az 0 el
out_pin:270 az 0 el
out_pin:292.5 az 0 el
out_pin:315 az 0 el
out_pin:337.5 az 0 el
out_pin:22.5 az 35.4 el
out_pin:67.5 az 35.4 el
out_pin:112.5 az 35.4 el
out_pin:157.5 az 35.4 el
out_pin:202.5 az 35.4 el
out_pin:247.5 az 35.4 el
out_pin:292.5 az 35.4 el
out_pin:337.5 az 35.4 el
out_pin:22.5 az -35.4 el
out_pin:67.5 az -35.4 el
out_pin:112.5 az -35.4 el
out_pin:157.5 az -35.4 el
out_pin:202.5 az -35.4 el
out_pin:247.5 az -35.4 el
out_pin:292.5 az -35.4 el
out_pin:337.5 az -35.4 el
@init
bpos=0;
csp1 = 1024;
length_of_csps = 4;
end_of_csps = csp1 + length_of_csps;
csp2 = end_of_csps;
end_of_csps = csp2 + length_of_csps;
csp3 = end_of_csps;
end_of_csps = csp3 + length_of_csps;
csp4 = end_of_csps;
end_of_csps = csp4 + length_of_csps;
csp5 = end_of_csps;
end_of_csps = csp5 + length_of_csps;
csp6 = end_of_csps;
end_of_csps = csp6 + length_of_csps;
csp7 = end_of_csps;
end_of_csps = csp7 + length_of_csps;
csp8 = end_of_csps;
end_of_csps = csp8 + length_of_csps;
csp9 = end_of_csps;
end_of_csps = csp9 + length_of_csps;
csp10 = end_of_csps;
end_of_csps = csp10 + length_of_csps;
csp11 = end_of_csps;
end_of_csps = csp11 + length_of_csps;
csp12 = end_of_csps;
end_of_csps = csp12 + length_of_csps;
csp13 = end_of_csps;
end_of_csps = csp13 + length_of_csps;
csp14 = end_of_csps;
end_of_csps = csp14 + length_of_csps;
csp15 = end_of_csps;
end_of_csps = csp15 + length_of_csps;
csp16 = end_of_csps;
end_of_csps = csp16 + length_of_csps;
csp17 = end_of_csps;
end_of_csps = csp17 + length_of_csps;
csp18 = end_of_csps;
end_of_csps = csp18 + length_of_csps;
csp19 = end_of_csps;
end_of_csps = csp19 + length_of_csps;
csp20 = end_of_csps;
end_of_csps = csp20 + length_of_csps;
csp21 = end_of_csps;
end_of_csps = csp21 + length_of_csps;
csp22 = end_of_csps;
end_of_csps = csp22 + length_of_csps;
csp23 = end_of_csps;
end_of_csps = csp23 + length_of_csps;
csp24 = end_of_csps;
end_of_csps = csp24 + length_of_csps;
csp25 = end_of_csps;
end_of_csps = csp25 + length_of_csps;
csp26 = end_of_csps;
end_of_csps = csp26 + length_of_csps;
csp27 = end_of_csps;
end_of_csps = csp27 + length_of_csps;
csp28 = end_of_csps;
end_of_csps = csp28 + length_of_csps;
csp29 = end_of_csps;
end_of_csps = csp29 + length_of_csps;
csp30 = end_of_csps;
end_of_csps = csp30 + length_of_csps;
csp31 = end_of_csps;
end_of_csps = csp31 + length_of_csps;
csp32 = end_of_csps;
end_of_csps = csp32 + length_of_csps;
csp1[0] = 0.0312500000;csp1[1] = 0.0000000000;csp1[2] = 0.0000000000;csp1[3] = 0.0751006353;
csp2[0] = 0.0312500000;csp2[1] = 0.0287397689;csp2[2] = 0.0000000000;csp2[3] = 0.0693839398;
csp3[0] = 0.0312500000;csp3[1] = 0.0531041685;csp3[2] = 0.0000000000;csp3[3] = 0.0531041685;
csp4[0] = 0.0312500000;csp4[1] = 0.0693839398;csp4[2] = -0.0000000000;csp4[3] = 0.0287397689;
csp5[0] = 0.0312500000;csp5[1] = 0.0751006353;csp5[2] = -0.0000000000;csp5[3] = -0.0000000000;
csp6[0] = 0.0312500000;csp6[1] = 0.0693839398;csp6[2] = -0.0000000000;csp6[3] = -0.0287397689;
csp7[0] = 0.0312500000;csp7[1] = 0.0531041685;csp7[2] = -0.0000000000;csp7[3] = -0.0531041685;
csp8[0] = 0.0312500000;csp8[1] = 0.0287397689;csp8[2] = -0.0000000000;csp8[3] = -0.0693839398;
csp9[0] = 0.0312500000;csp9[1] = 0.0000000000;csp9[2] = -0.0000000000;csp9[3] = -0.0751006353;
csp10[0] = 0.0312500000;csp10[1] = -0.0287397689;csp10[2] = -0.0000000000;csp10[3] = -0.0693839398;
csp11[0] = 0.0312500000;csp11[1] = -0.0531041685;csp11[2] = -0.0000000000;csp11[3] = -0.0531041685;
csp12[0] = 0.0312500000;csp12[1] = -0.0693839398;csp12[2] = 0.0000000000;csp12[3] = -0.0287397689;
csp13[0] = 0.0312500000;csp13[1] = -0.0751006353;csp13[2] = 0.0000000000;csp13[3] = 0.0000000000;
csp14[0] = 0.0312500000;csp14[1] = -0.0693839398;csp14[2] = 0.0000000000;csp14[3] = 0.0287397689;
csp15[0] = 0.0312500000;csp15[1] = -0.0531041685;csp15[2] = 0.0000000000;csp15[3] = 0.0531041685;
csp16[0] = 0.0312500000;csp16[1] = -0.0287397689;csp16[2] = 0.0000000000;csp16[3] = 0.0693839398;
csp17[0] = 0.0312500000;csp17[1] = 0.0234265845;csp17[2] = 0.1078923379;csp17[3] = 0.0565567779;
csp18[0] = 0.0312500000;csp18[1] = 0.0565567779;csp18[2] = 0.1078923379;csp18[3] = 0.0234265845;
csp19[0] = 0.0312500000;csp19[1] = 0.0565567779;csp19[2] = 0.1078923379;csp19[3] = -0.0234265845;
csp20[0] = 0.0312500000;csp20[1] = 0.0234265845;csp20[2] = 0.1078923379;csp20[3] = -0.0565567779;
csp21[0] = 0.0312500000;csp21[1] = -0.0234265845;csp21[2] = 0.1078923379;csp21[3] = -0.0565567779;
csp22[0] = 0.0312500000;csp22[1] = -0.0565567779;csp22[2] = 0.1078923379;csp22[3] = -0.0234265845;
csp23[0] = 0.0312500000;csp23[1] = -0.0565567779;csp23[2] = 0.1078923379;csp23[3] = 0.0234265845;
csp24[0] = 0.0312500000;csp24[1] = -0.0234265845;csp24[2] = 0.1078923379;csp24[3] = 0.0565567779;
csp25[0] = 0.0312500000;csp25[1] = 0.0234265845;csp25[2] = -0.1078923379;csp25[3] = 0.0565567779;
csp26[0] = 0.0312500000;csp26[1] = 0.0565567779;csp26[2] = -0.1078923379;csp26[3] = 0.0234265845;
csp27[0] = 0.0312500000;csp27[1] = 0.0565567779;csp27[2] = -0.1078923379;csp27[3] = -0.0234265845;
csp28[0] = 0.0312500000;csp28[1] = 0.0234265845;csp28[2] = -0.1078923379;csp28[3] = -0.0565567779;
csp29[0] = 0.0312500000;csp29[1] = -0.0234265845;csp29[2] = -0.1078923379;csp29[3] = -0.0565567779;
csp30[0] = 0.0312500000;csp30[1] = -0.0565567779;csp30[2] = -0.1078923379;csp30[3] = -0.0234265845;
csp31[0] = 0.0312500000;csp31[1] = -0.0565567779;csp31[2] = -0.1078923379;csp31[3] = 0.0234265845;
csp32[0] = 0.0312500000;csp32[1] = -0.0234265845;csp32[2] = -0.1078923379;csp32[3] = 0.0565567779;

MaxRvGain2d = end_of_csps;
length_of_array = 2;
end_of_gain = MaxRvGain2d + length_of_array;
MaxRvGain2d[0] = 1.000;MaxRvGain2d[1] = 1.000;
MaxRvGain3d = end_of_gain;
length_of_array = 2;
end_of_gain = MaxRvGain3d + length_of_array;
MaxRvGain3d[0] = 1.000;MaxRvGain3d[1] = 1.000;
InPhase2d = end_of_gain;
length_of_array = 2;
end_of_gain = InPhaseGain2d + length_of_array;
InPhaseGain2d[0] = 1.0000;InPhaseGain2d[1] = 0.5000;
InPhaseGain3d = end_of_gain;
length_of_array = 2;
end_of_gain = InPhaseGain3d + length_of_array;
InPhaseGain3d[0] = 1.0000;InPhaseGain3d[1] = 0.3333;
MaxReGain2d = end_of_gain;
length_of_array = 2;
end_of_gain = MaxReGain2d + length_of_array;
MaxReGain2d[0] = 1.0000;MaxReGain2d[1] = 0.7071;
MaxReGain3d = end_of_gain;
length_of_array = 2;
end_of_gain = MaxReGain3d + length_of_array;
MaxReGain3d[0] = 1.0000;MaxReGain3d[1] = 0.5774;

BF = end_of_gain;
length_of_array = 4;
end_of_array = BF + length_of_array;
function SetDistCoef(dNFC,dspeak) local(w1,w2)
(
	NFCDist == 4.0? w1 = 0 :(
		w1 = 342.0/(dNFC*srate);
	);
	w2 = 342.0/(dspeak*srate);
	NF1.NF_Filt1Init(w1,w2,1.0);
	NF2.NF_Filt1Init(w1,w2,1.0);
	NF3.NF_Filt1Init(w1,w2,1.0);
);

SF0.shelfinit(srate,1200.0,1.0,1.0,1);
SF1.shelfinit(srate,1200.0,1.0,1.0,1);
SF2.shelfinit(srate,1200.0,1.0,1.0,1);
SF3.shelfinit(srate,1200.0,1.0,1.0,1);
NF1.NF_Filt1Init(342.0/(NFCDist*srate),342.0/(SpDist*srate),1.0);
NF2.NF_Filt1Init(342.0/(NFCDist*srate),342.0/(SpDist*srate),1.0);
NF3.NF_Filt1Init(342.0/(NFCDist*srate),342.0/(SpDist*srate),1.0);

@slider
slider1==0 ? GainTypeLF = MaxRvGain3d;
slider1==1 ? GainTypeLF = InPhaseGain3d;
slider1==2 ? GainTypeLF = MaxReGain3d;
slider2==0 ? GainTypeHF = MaxRvGain3d;
slider2==1 ? GainTypeHF = InPhaseGain3d;
slider2==2 ? GainTypeHF = MaxReGain3d;

LFGainLin = 2 ^(LFGain/6);
HFGainLin = 2 ^(HFGain/6);

SF0.shelfinit(srate,slider3,GainTypeLF[0]*LFGainLin,GainTypeHF[0]*HFGainLin,0);
SF1.shelfinit(srate,slider3,GainTypeLF[1]*LFGainLin,GainTypeHF[1]*HFGainLin,0);
SF2.shelfinit(srate,slider3,GainTypeLF[1]*LFGainLin,GainTypeHF[1]*HFGainLin,0);
SF3.shelfinit(srate,slider3,GainTypeLF[1]*LFGainLin,GainTypeHF[1]*HFGainLin,0);
SetDistCoef(NFCDist,SpDist);

@sample
BF[0] = SF0.shelf(spl0);
BF[1] = SF1.shelf(spl1);
BF[2] = SF2.shelf(spl2);
BF[3] = SF3.shelf(spl3);

DistFiltEnable!=0? (
	DistFiltEnable==1?(
		BF[1]=NF1.NF_Filt1ProcessSpDist(BF[1]);
		BF[2]=NF2.NF_Filt1ProcessSpDist(BF[2]);
		BF[3]=NF3.NF_Filt1ProcessSpDist(BF[3]);
	) : (
		BF[1]=NF1.NF_Filt1Process(BF[1]);
		BF[2]=NF2.NF_Filt1Process(BF[2]);
		BF[3]=NF3.NF_Filt1Process(BF[3]);
	);
);

spl0=spl1=spl2=spl3=spl4=spl5=spl6=spl7=spl8=spl9=spl10=spl11=spl12=spl13=spl14=spl15=spl16=spl17=spl18=spl19=spl20=spl21=spl22=spl23=spl24=spl25=spl26=spl27=spl28=spl29=spl30=spl31=0.0;

cnt = 0;
loop(4,
spl0+=BF[cnt]*csp1[cnt];
spl1+=BF[cnt]*csp2[cnt];
spl2+=BF[cnt]*csp3[cnt];
spl3+=BF[cnt]*csp4[cnt];
spl4+=BF[cnt]*csp5[cnt];
spl5+=BF[cnt]*csp6[cnt];
spl6+=BF[cnt]*csp7[cnt];
spl7+=BF[cnt]*csp8[cnt];
spl8+=BF[cnt]*csp9[cnt];
spl9+=BF[cnt]*csp10[cnt];
spl10+=BF[cnt]*csp11[cnt];
spl11+=BF[cnt]*csp12[cnt];
spl12+=BF[cnt]*csp13[cnt];
spl13+=BF[cnt]*csp14[cnt];
spl14+=BF[cnt]*csp15[cnt];
spl15+=BF[cnt]*csp16[cnt];
spl16+=BF[cnt]*csp17[cnt];
spl17+=BF[cnt]*csp18[cnt];
spl18+=BF[cnt]*csp19[cnt];
spl19+=BF[cnt]*csp20[cnt];
spl20+=BF[cnt]*csp21[cnt];
spl21+=BF[cnt]*csp22[cnt];
spl22+=BF[cnt]*csp23[cnt];
spl23+=BF[cnt]*csp24[cnt];
spl24+=BF[cnt]*csp25[cnt];
spl25+=BF[cnt]*csp26[cnt];
spl26+=BF[cnt]*csp27[cnt];
spl27+=BF[cnt]*csp28[cnt];
spl28+=BF[cnt]*csp29[cnt];
spl29+=BF[cnt]*csp30[cnt];
spl30+=BF[cnt]*csp31[cnt];
spl31+=BF[cnt]*csp32[cnt];
cnt = cnt+1;
);

@gfx 724 310
gfx_r = 1.0;gfx_g =1.0;gfx_b = 1.0;
gfx_x = 3;
gfx_y = 3;
gfx_drawnumber(GainTypeLF[0]*LFGainLin,3);gfx_drawstr(" : ");
gfx_drawnumber(GainTypeLF[1]*LFGainLin,3);gfx_drawstr(" LF Per Order Gain Values");
gfx_x = 3;
gfx_y += gfx_texth+3;
gfx_drawnumber(GainTypeHF[0]*HFGainLin,3);gfx_drawstr(" : ");
gfx_drawnumber(GainTypeHF[1]*HFGainLin,3);gfx_drawstr(" HF Per Order Gain Values");
gfx_x = 3;
gfx_y += gfx_texth+3;
gfx_drawstr("Sp Dist Comp set to: ");
DistFiltEnable>=1?(gfx_drawnumber(SpDist+0.005,2);gfx_drawchar('m');) : gfx_drawstr("off");
gfx_x = 3;
gfx_y += gfx_texth+3;
gfx_drawstr("NFC set to: ");
DistFiltEnable==2?(gfx_drawnumber(NFCDist+0.005,2);gfx_drawchar('m');) : gfx_drawstr("off");

gfx_x = 0;
gfx_y+=gfx_texth+3;
gfx_blit(0, 1.0, 0.0);
gfx_x+=245;
gfx_blit(1,1.0,0.0);
gfx_x+=245;
gfx_blit(2,1.0,0.0);
gfx_x = 600;
gfx_y = 5;
gfx_blit(3, 1.0, 0.0);
